<template>
  <div ref="wrapper" class="wrapper" :style="wrapperStyle" >
    <div class="content" :style="contentStyle">
      <div v-if='pulldown'
        class="pulldown box vertical-center horizontal-center"
      >
        <img class="pulldownImg" 
          :src="refreshIconData"
          :class="imgRotate?'imgRotate':''"  
        >
          <p>{{refreshTextData}}</p>
      </div>
      <slot></slot>
      <div class="pulldown box vertical-center horizontal-center">
        <img v-show='loadingShowData'
            class="pulldownImg" 
            src="../assets/images/loading1.gif" 
        >
        <p>{{loadTextData}}</p>
      </div>
    </div>
  </div>
</template>
<script>
  import BScroll from 'better-scroll'
  export default {
    data () {
      return {
        refreshIconData: this.refreshIcon,
        refreshTextData: this.refreshText,
        loadingShowData: this.loadingShow,
        loadTextData: this.loadText,
        imgRotate: false,
        distance: 0
      }
    },

    watch: {
      // 监听数据的变化，延时refreshDelay时间后调用refresh方法重新计算，保证滚动效果正常
      data () {
        setTimeout(() => {
          this.refresh()
        }, this.refreshDelay)
      },
      refreshFlag(){
        setTimeout(() => {
          this.refresh()
        }, 20)
      },
      refreshIcon (val) {
        this.refreshIconData = val;
      },
      refreshText (val) {
        this.refreshTextData = val;
      },
      loadingShow (val) {
        this.loadingShowData = val;
      },
      loadText (val) {
        this.loadTextData = val;
      }
    },
    props: {
      /**
       * 1 滚动的时候会派发scroll事件，会截流。
       * 2 滚动的时候实时派发scroll事件，不会截流。
       * 3 除了实时派发scroll事件，在swipe的情况下仍然能实时派发scroll事件
       */
      probeType: {
        type: Number,
        default: 2
      },
      /**
       * 点击列表是否派发click事件
       */
      click: {
        type: Boolean,
        default: true
      },
      /**
       * 是否开启横向滚动
       */
      scrollX: {
        type: Boolean,
        default: false
      },
      /**
       * 是否派发滚动事件
       */
      listenScroll: {
        type: Boolean,
        default: true
      },
      /**
       * 列表的数据
       */
      data: {
        type: Array || String,
        default: () => {
          return [];
        }
      },
      /**
       * 是否派发滚动到底部的事件，用于上拉加载
       */
      pullup: {
        type: Boolean,
        default: false
      },
      /**
       * 是否派发顶部下拉的事件，用于下拉刷新
       */
      pulldown: {
        type: Boolean,
        default: false
      },
      /**
       * 是否派发列表滚动开始的事件
       */
      beforeScroll: {
        type: Boolean,
        default: false
      },
      /**
       * 当数据更新后，刷新scroll的延时。
       */
      refreshDelay: {
        type: Number,
        default: 20
      },
      /*
       * wrapper style
       * */
      wrapperStyle: {
        type: String,
        default: ""
      },
      /*
       * content style
       * */
      contentStyle: {
        type: String,
        default: ""
      },
      refreshFlag: {
        type: Boolean,
        default: false
      },
      refreshIcon: {
        type: String,
        default: ''
      },
      refreshText: {
        type: String,
        default: ''
      },
      loadingShow: {
        type: Boolean,
        default: false
      },
      loadText: {
        type: String,
        default: ''
      }
    },

    mounted () {
      // 保证在DOM渲染完毕后初始化better-scroll
      setTimeout(() => {
        if (this.pullup || this.pulldown) this.distance = document.getElementsByClassName('pulldown')[0].offsetHeight;
        this._initScroll()
      }, this.refreshDelay)
    },
    methods: {
      _initScroll () {
        if (!this.$refs.wrapper) {
          return
        }
        // better-scroll的初始化
        this.scroll = new BScroll(this.$refs.wrapper, {
          probeType: this.probeType,
          click: this.click,
          scrollX: this.scrollX
        })

        // 是否派发滚动事件
        if (this.listenScroll) {
          this.scroll.on('scroll', (pos) => {
            if (this.pulldown) {
              if (pos.y > 0 && pos.y < this.distance) {
                this.imgRotate = false
              } else if (pos.y >= this.distance) {
                this.imgRotate = true
              }
            }
            this.$emit('scroll', pos)
          })
        }

        // 是否派发顶部下拉事件，用于下拉刷新
        if (this.pulldown) {
          this.scroll.on('touchEnd', (pos) => {
            // 下拉动作
            if (pos.y > 0) {
              if (pos.y >= this.distance) {
                this.$emit('pulldown')
              } else {
                this.scrollTo(0, 0, 100)
              }
            }
          })
        }

        // 是否派发列表滚动开始的事件
        if (this.beforeScroll) {
          this.scroll.on('beforeScrollStart', () => {
            this.$emit('beforeScroll')
          })
        }
        // 是否派发列表滚动结束的事件
        if (this.pulldown || this.pullup) {
          this.scroll.on('scrollEnd', (pos) => {
            if (pos.y > 0 && pos.y <= this.distance && !this.imgRotate) {
              this.scrollTo(0, 0, 100)
            }
            if (this.pullup) { // 是否派发滚动到底部事件，用于上拉加载
              if (this.scroll.y <= (this.scroll.maxScrollY)) {
                this.$emit('loadMore')
              }
            }
          })
        }
      },
      changeReImgRotate() {
        this.imgRotate = false
      },
      disable () {
        // 代理better-scroll的disable方法
        this.scroll && this.scroll.disable()
      },
      enable () {
        // 代理better-scroll的enable方法
        this.scroll && this.scroll.enable()
      },
      refresh () {
        // 代理better-scroll的refresh方法
        this.scroll && this.scroll.refresh()
      },
      scrollTo () {
        // 代理better-scroll的scrollTo方法
        this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
      },
      scrollToElement () {
        // 代理better-scroll的scrollToElement方法
        this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
      }
    },

  }
</script>
<style lang="less">
  .msg {
    height: 4rem;
    font-size: 1rem;
    color: #828B93;
  }

  .msg > div {
    margin-right: 10px;
  }
  .content{
    position: relative;
  }
  .scrollData{
    width: 90%;
    height: 2rem;
    line-height: 2rem;
    text-align: center;
    border-radius: 5px;
    box-shadow: 0 2px 10px rgba(0,0,0,.2);
    margin-bottom: 1rem;
    margin-left: auto;
    margin-right: auto
}
.pulldown{
    width: 100%;
    height: 2.5rem;
    .pulldownImg{
      width: 1.2rem;
      height: 1.2rem;
      margin-right: 1rem;
      transition: all 0.2s ease-in;
      display: block;
    }
    .imgRotate{
      transform: rotateZ(180deg)
    }
    p{
      height: 1.5rem;
      line-height: 1.5rem;
    }
}
</style>
